﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Koonsoft.HYCRM.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    public class FileProcessDetailDAL
    {
        private Database database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        public List<FileProcessDetailInfo> GetDetailsByOrderID(string orderID)
        {
            var fileProcessDetails = new List<FileProcessDetailInfo>();

            const string sql = @"select a.*,b.ProcessContent as FileProcessStep from (select * from FileProcessdetail where OrderID=@orderid) as a 
                                join FileProcess b 
                                on a.FileProcessID=b.FileProcessID order by Priority";

            using (var cmd = database.GetSqlStringCommand(sql))
            {
                database.AddInParameter(cmd, "@orderid", DbType.String, orderID);

                var rd = (RefCountingDataReader)database.ExecuteReader(cmd);
                var sqlrd = (SqlDataReader)rd.InnerReader;

                while (sqlrd.Read())
                {
                    fileProcessDetails.Add(new FileProcessDetailInfo
                                               {
                                                   DetailID = Convert.ToInt32(sqlrd["DetailID"]),
                                                   OrderID = sqlrd["OrderID"].ToString(),
                                                   FileProcessID = Convert.ToInt32(sqlrd["FileProcessID"]),
                                                   FinishedTime = sqlrd["FinishedTime"].ToString(),
                                                   Remark = sqlrd["Remark"].ToString(),
                                                   Priority = Convert.ToInt32(sqlrd["Priority"]),
                                                   FileProcessStep = sqlrd["FileProcessStep"].ToString()
                                               });
                }
            }

            return fileProcessDetails;
        }
    }
}
